<?php

namespace app\admin\controller;
use think\Image;

class Index extends base
{

    public function _initialize()
    {
        //覆盖原来的自动载入
    }
    /**
     * 读取文件目录
     * @param $basedir 文件地址
     */
    function checkdir($basedir)
    {
        static $filename = array();
        if ($dh = opendir($basedir)) {
            while (($file = readdir($dh)) !== false) {
                $tmparray = explode('.ioc',$file);
                if ($file != '.' && $file != '..') {

                    if (!is_dir($basedir . "/" . $file)) {
                        $filename[] = ['filename' => "$file", 'type' => $this->request->param('type'), 'dir' => $this->request->param('dir'), 'name' => $file];
                    } else {
                        $dirname = $basedir . "/" . $file;

                        $this->checkdir($dirname);
                    }
                }
            }
        }
        return $filename;
    }

    public function index()
    {
//        echo    ROOT_PATH . 'public' . DS .'uploads/image' . DS ;
//
//        $ll = $this->checkdir(ROOT_PATH . 'public' . DS .'uploads/png' . DS);
//        foreach ($ll as $item=>$value) {
//
//            $image = Image::open('uploads/png/'.$value['filename']);
//            $image->thumb('1200','500')->save('uploads/png/'.$value['filename']);
//        }
//
//exit();
        return $this->fetch();
    }

    /**
     * 欢迎页面
     */
    public function welcome()
    {
        $data['lang'] = cookie('think_var_admin');

        $ucase = model('ucase')->where($data)->count();
        $product = model('product')->where($data)->count();
        $article = model('article')->where('contribute','0')->where($data)->count();
        $articles = model('article')->where('contribute','1')->where($data)->count();
        $purchase = model('purchase')->where($data)->count();
        $jobapply = model('jobapply')->where($data)->count();
        //删除多余的数据登陆数据一个月
        $del30 = cache('del30');
        if(empty($del30)){
            $nowtime = time();
            //删除一月的访问记录
            $yiyue = 2592000;
            $deltime = $yiyue * 12;
            model('visit_summary')->where('create_time','<',$nowtime-$deltime)->where('antepage',null)->where('network',null)->delete();

            //删除一年的操作记录
            $deltime = $yiyue * 12;
            model('Weblog')->where('create_at','<',$nowtime-$deltime)->delete();
            cache('del30','del');
        }
        $this->assign([
            'ucase' => $ucase,
            'product' => $product,
            'article' => $article,
            'articles' => $articles,
            'purchase' => $purchase,
            'jobapply' => $jobapply,
            'systemConfig' => $this->systemConfig(),

        ]);
        return $this->fetch();
    }

    /**
     * @return array 服务器信息
     */
    private function systemConfig()
    {
        $config = cache('systemConfig');
        if(empty($config)){
            $config = [
                '操作系统' => PHP_OS,
                '运行环境' => $_SERVER["SERVER_SOFTWARE"],
               'PHP运行方式' => php_sapi_name(),
                '上传附件限制' => ini_get('upload_max_filesize'),
                '执行时间限制' => ini_get('max_execution_time') . '秒',
               '服务器IP地址' => $this->request->ip(),
                '服务器域名' => $this->request->domain(),
               '服务器端口 ' => $_SERVER['SERVER_PORT'],
//                '本文件所在文件夹' => $_SERVER['SCRIPT_FILENAME'],
//                '系统所在文件夹' => $_SERVER['DOCUMENT_ROOT'],
        //    '磁盘剩余空间 ' => round((@disk_free_space(".") / (1024 * 1024)), 2) . 'M',
            ];
            cache('systemConfig',$config);
        }
        return $config;
    }


    /**
     * @return 清理缓存
     */
    public function cleanCache()
    {
        if($this->request->isAjax()) {
            deldir(RUNTIME_PATH);
            $this->redirect($this->request->domain());
//            return ajaxMsg('1', '成功清理缓存！');
        }

    }


    /**
     * @return 自动选择语言
     */
    public function autolang()
    {
        if($this->request->isAjax()) {
            $value =  $this->request->param('value');
            db('system')->where('title','autolang')->update(['value' => $value]);
            cookie('autolang', $value);
//            return $value;
            //$this->redirect($this->request->domain());
        }

    }

    public function lang() {
        switch ($this->request->param('lang')) {
            case 'en':
                cookie('think_var_admin', 'en-us');
                break;
            case 'cn':
            default:
                cookie('think_var_admin', 'zh-cn');
                break;
            //其它语言
        }
        $this->redirect($this->request->server('HTTP_REFERER'));
    }
}
